using System;
using System.Text;

public static class ArrayExt
{
    public static string ToHexString(this byte[] value, int index = 0)
    {
        return ToHexString(value, index, value.Length - index);
    }

    public static string ToHexStringReversed(this byte[] value, int index = 0)
    {
        if (value == null) return null;

        byte[] rev = new byte[value.Length];

#if MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_2
        for (var i = 0; i < value.Length; i++) rev[i] = value[value.Length - 1 - i];
#else
        Array.Copy(value, rev, value.Length);
        Array.Reverse(rev);
#endif

            return ToHexString(rev, index, value.Length - index);
    }

    public static int ToIntValue(this byte[] buffer, int offset, int length)
    {
        var val = 0;
        for (var i = offset; i < offset + length; ++i)
        {
            val += buffer[i];
            if (i < offset + length - 1)
                val = val << 8;
        }
        return val;
    }

    public static string ToHexString(this byte[] value, int index, int length)
    {
        if (value == null) return null;

        if (length == 0) return string.Empty;

        char[] c = new char[length * 3];
        byte b;

        for (int y = 0, x = 0; y < length; ++y, ++x)
        {
            b = (byte)(value[index + y] >> 4);
            c[x] = (char)(b > 9 ? b + 0x37 : b + 0x30);
            b = (byte)(value[index + y] & 0xF);
            c[++x] = (char)(b > 9 ? b + 0x37 : b + 0x30);
            c[++x] = ':';
        }
        return new string(c, 0, c.Length - 1);
    }

    public static string ToFormattedString(this bool[] value)
    {
        if (value.Length == 0) return string.Empty;

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < value.Length; i++)
        {
            sb.Append(value[i].ToString());
            if (i != value.Length - 1) sb.Append(",");
        }

        return sb.ToString();
    }

    public static string ToFormattedString(this float[] value)
    {
        if (value.Length == 0) return string.Empty;

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < value.Length; i++)
        {
            sb.Append(value[i].ToString());
            if (i != value.Length - 1) sb.Append(",");
        }

        return sb.ToString();
    }
}